package com.gxa.hotel.admin.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.conditions.update.UpdateChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.gxa.hotel.admin.service.RoomOrderService;
import com.gxa.hotel.admin.service.RoomOrderlistService;
import com.gxa.hotel.core.common.JsonResult;

import com.gxa.hotel.core.pojo.domain.RoomOrder;
import com.gxa.hotel.core.pojo.vo.RoomOrderlistVO;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hotel
 * @since 2022-07-20
 */
@ApiModel(value = "客房订单")
@RestController

public class RoomOrderController {

    @Autowired
    RoomOrderService roomOrderService;

    @Autowired
    RoomOrderlistService roomOrderlistService;

    LambdaQueryWrapper<RoomOrder> lambda= null;
    LambdaQueryWrapper<RoomOrderlistVO> lambdaVO=null;
    @ApiOperation(value = "客房订单首页分页查询",produces = "query")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "页数",paramType = "query"),
            @ApiImplicitParam(name = "size",value = "每页记录数",paramType = "query"),


    })
    @PostMapping("/orderPage")
    public Map selectByOrderPage(
            @RequestParam(value = "page",required = true)Integer page,
            @RequestParam(value = "size",required = true)Integer size
    ){

        Page<RoomOrder> page1 = new Page<>(page, size);
        Page<RoomOrder> result = roomOrderService.page(page1);

        JsonResult jsonResult=new JsonResult();

        jsonResult.setCode("200");
        jsonResult.setMsg("查询成功");
        //数据
        jsonResult.setData(result.getRecords());
        //总记录数
        jsonResult.set("count",result.getTotal());
        return jsonResult.getMap();
    }

    @ApiOperation(value = "客房订单动态条件查询",produces = "query")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "页数",paramType = "query"),
            @ApiImplicitParam(name = "size",value = "每页记录数",paramType = "query"),
            @ApiImplicitParam(name = "state",value = "订单状态",paramType = "query"),
            @ApiImplicitParam(name = "from",value = "起始时间",paramType = "query"),
            @ApiImplicitParam(name = "to",value = "截止时间",paramType = "query"),
            @ApiImplicitParam(name = "orderId",value = "订单编号",paramType = "query"),
            @ApiImplicitParam(name = "name",value = "收货人姓名",paramType = "query"),
            @ApiImplicitParam(name = "phone",value = "收货人电话",paramType = "query"),

    })
    @PostMapping("/selectByName")
    public Map selectByName(
            @RequestParam(value = "state",required = false)Integer state,
            @RequestParam(value = "from",required = false)String from,
            @RequestParam(value = "to",required = false)String to,
            @RequestParam(value = "orderId",required = false)String orderId,
            @RequestParam(value = "name",required = false)String name,
            @RequestParam(value = "phone",required = false)String phone,
            @RequestParam(value = "page",required = true)Integer page,
            @RequestParam(value = "size",required = true)Integer size
    ){

        String static1= String.valueOf(state);

        if (state==null){
            static1="";
        }
        lambda=Wrappers.<RoomOrder>lambdaQuery();
        lambda.eq(StringUtils.isNotBlank(static1),RoomOrder::getRoState,state)
                .gt(StringUtils.isNotBlank(from),RoomOrder::getRoInTime,from)
                .lt(StringUtils.isNotBlank(to),RoomOrder::getRoOutTime,to)
                .like(StringUtils.isNotBlank(orderId),RoomOrder::getRoId,orderId)
                .like(StringUtils.isNotBlank(name),RoomOrder::getRoName,name)
                .like(StringUtils.isNotBlank(phone),RoomOrder::getRoPhone,phone);
        Page<RoomOrder> page1 = new Page<>(page, size);
        Page<RoomOrder> result = roomOrderService.page(page1,lambda);

        JsonResult jsonResult=new JsonResult();

        jsonResult.setCode("200");
        jsonResult.setMsg("查询成功");
        //数据
        jsonResult.setData(result.getRecords());
        //总记录数
        jsonResult.set("count",result.getTotal());
        return jsonResult.getMap();
    }


    @ApiOperation(value = "删除订单",produces = "query")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "删除的订单号",paramType = "query"),



    })
    @PostMapping("/deleteById")
    public Map deleteById(
            @RequestParam(value = "id",required = true)String id

    ){
        lambda.eq(RoomOrder::getRoId,id);

        boolean remove = roomOrderService.remove(lambda);

        JsonResult jsonResult=new JsonResult();
        if (remove){
            jsonResult.setCode("200");
            jsonResult.setMsg("删除成功");
        }else {
            jsonResult.setCode("0");
            jsonResult.setMsg("删除失败");
        }


        return jsonResult.getMap();
    }
    @ApiOperation(value = "确认入住",produces = "query")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "确认的订单号",paramType = "query"),



    })
    @PostMapping("/confirm")
    public Map confirm(
            @RequestParam(value = "id",required = true)String id

    ){
        lambda=Wrappers.<RoomOrder>lambdaQuery();
        lambda.eq(RoomOrder::getRoId,id);
        RoomOrder roomOrder = roomOrderService.getOne(lambda);
        roomOrder.setRoState(2);
        boolean b = roomOrderService.saveOrUpdate(roomOrder);

        JsonResult jsonResult=new JsonResult();
        if (b){
            jsonResult.setCode("200");
            jsonResult.setMsg("修改成功");
        }else {
            jsonResult.setCode("0");
            jsonResult.setMsg("修改失败");
        }


        return jsonResult.getMap();
    }

    @ApiOperation(value = "住宿记录分页查询",produces = "query")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "页数",paramType = "query"),
            @ApiImplicitParam(name = "size",value = "每页记录数",paramType = "query"),


    })
    @PostMapping("/RecordPage")
    public Map selectByPage(
            @RequestParam(value = "page",required = true)Integer page,
            @RequestParam(value = "size",required = true)Integer size
    ){

        Page<RoomOrderlistVO> page1 = new Page<>(page, size);
        Page<RoomOrderlistVO> result = roomOrderlistService.page(page1);

        JsonResult jsonResult=new JsonResult();

        jsonResult.setCode("200");
        jsonResult.setMsg("查询成功");
        //数据
        jsonResult.setData(result.getRecords());
        //总记录数
        jsonResult.set("count",result.getTotal());
        return jsonResult.getMap();
    }
    @ApiOperation(value = "住宿记录动态条件查询",produces = "query")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "页数",paramType = "query"),
            @ApiImplicitParam(name = "size",value = "每页记录数",paramType = "query"),

            @ApiImplicitParam(name = "from",value = "起始时间",paramType = "query"),
            @ApiImplicitParam(name = "to",value = "截止时间",paramType = "query"),
            @ApiImplicitParam(name = "name",value = "模糊查询",paramType = "query"),


    })
    @PostMapping("/selectByRecord")
    public Map selectByRecord(

            @RequestParam(value = "from",required = false)String from,
            @RequestParam(value = "to",required = false)String to,
            @RequestParam(value = "name",required = false)String name,

            @RequestParam(value = "page",required = true)Integer page,
            @RequestParam(value = "size",required = true)Integer size
    ){
        lambdaVO= Wrappers.<RoomOrderlistVO>lambdaQuery();
        lambdaVO.gt(StringUtils.isNotBlank(from),RoomOrderlistVO::getRoInTime,from)
                .lt(StringUtils.isNotBlank(to),RoomOrderlistVO::getRoOutTime,to)

                .like(StringUtils.isNotBlank(name),RoomOrderlistVO::getRId,name).or()
                .like(StringUtils.isNotBlank(name),RoomOrderlistVO::getRoName,name).or()
                .like(StringUtils.isNotBlank(name),RoomOrderlistVO::getRoPhone,name);

        Page<RoomOrderlistVO> page1 = new Page<>(page, size);

        Page<RoomOrderlistVO> result = roomOrderlistService.page(page1,lambdaVO);

        JsonResult jsonResult=new JsonResult();

        jsonResult.setCode("200");
        jsonResult.setMsg("查询成功");
        //数据
        jsonResult.setData(result.getRecords());
        //总记录数
        jsonResult.set("count",result.getTotal());
        return jsonResult.getMap();
    }
    @ApiOperation(value = "导出接口")
    @PostMapping("/exportPart")
    public Map exportPart2(HttpServletResponse response, String... id) throws Exception {
        lambdaVO= Wrappers.<RoomOrderlistVO>lambdaQuery();
        List<RoomOrderlistVO> list=new ArrayList();
        if (id!=null ){
            for (int i = 0; i < id.length; i++) {
                RoomOrderlistVO roomOrderlistVO = roomOrderlistService.getById(id[i]);
                if(roomOrderlistVO!=null){
                    list.add(roomOrderlistVO);
                }

            }
        }else{
            List<RoomOrderlistVO> list1 = roomOrderlistService.list();
            for (int i = 0; i <list1.size() ; i++) {
                list.add(list1.get(i));
            }
        }


        roomOrderlistService.export(list,response);

        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("200");
        return  jsonResult.getMap();

    }
    @ApiOperation(value = "查看订单",produces = "query")

    @PostMapping("/selectAll")
    public Map selectAll(){

        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("200");
        return jsonResult.getMap();
    }
}

